package com.lianjinsu.shop.task;

import com.lianjinsu.shop.entity.Orders;
import com.lianjinsu.shop.entity.enums.OrderStatus;
import com.lianjinsu.shop.repository.dao.OrdersDao;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
@RequiredArgsConstructor
public class UpdateOrderStatusTask {

    private final OrdersDao ordersDao;

    @Scheduled(cron = "0 0/1 * * * ?")
    void orderStatusTask() {
        String before24H = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<Orders> ordersList = ordersDao.selectListOvertime(before24H);
        ordersList.forEach(e -> e.setStatus(OrderStatus.UNPAID_TIMEOUT));
        ordersDao.updateBatchById(ordersList);
    }
}
